package hdeiles

import (
	"edo-era-dev/cache"
	"edo-era-dev/models"
	"edo-era-dev/pkg"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	user := c.PostForm("user")
	passes := c.PostForm("passes")

	if user == "" || passes == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msh":  "字段不能为空",
		})
		return
	}
	redis_key := "redis_login" + user
	po, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if po >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误3次,禁止登录",
		})
		return
	}
	var users models.User
	models.DB.Where("user=?", user).Find(&users)
	if users.ID == 0 {
		c.JSON(200, gin.H{
			"code": "500",
			"mag":  "用户不存在",
		})
		return
	}
	if users.Passes != pkg.Md5(passes) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if po == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误3次,2个小时禁止登录",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": "500",
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": users,
	})
	return
}
func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	aew := c.PostForm("aew")

	if title == "" || content == "" || aew == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Ares{
		Title:   title,
		Content: content,
		Aew:     aew,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	redis_key := "redis_add" + strconv.Itoa(int(data.ID))
	var are models.Ares
	models.DB.Where("id=?", data.ID).First(&are)
	jsons, _ := json.Marshal(are)
	cache.Rdb.Set(cache.Ctx, redis_key, jsons, time.Hour*2)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size

	var count int64
	models.DB.Model(&models.Ares{}).Count(&count)

	var po []models.Ares
	err := models.DB.Offset(offset).Limit(size).Find(&po).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
		})
		return
	}

	redis_key := "redis_list" + strconv.Itoa(page)
	i, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if i == "" {
		models.DB.Offset(offset).Limit(size).Find(&po)
		marshal, _ := json.Marshal(po)
		cache.Rdb.Set(cache.Ctx, redis_key, marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(i), &po)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"count": count,
			"po":    po,
		},
	})
	return
}
func Xq(c *gin.Context) {
	id := c.Query("id")
	var ar models.Ares

	redis_key := "redis_xq" + id
	i, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if i == "" {
		models.DB.Where("id=?", id).First(&ar)
		marshal, _ := json.Marshal(ar)
		cache.Rdb.Set(cache.Ctx, redis_key, marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(i), &ar)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": ar,
	})
	return
}
func Del(c *gin.Context) {
	id := c.Query("id")

	redis_key := "redis_add" + id
	cache.Rdb.Del(cache.Ctx, redis_key)

	models.DB.Where("id=?", id).Delete(&models.Ares{})
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
	})
	return
}
